
function http_post( req, uri )
	local get_query, split_uri = getQuery(uri)
	local content_type = findHeader(req, "Content-Type")

--	setContentType( req, uri )
	if content_type == nil or content_type ~= "application/x-www-form-urlencoded" then
		return
	end

	local post_content = decodeURIQuery(getBody(req))
	
	-- just dumps all of the request right now:
	print("\nContent-Type = "..content_type)
	print("URI: "..uri)
	
	for i,v in ipairs(split_uri) do
		print(i,v)
	end
	
	print("Get query parts:")
	for k,v in pairs(get_query) do
		print(k.." = "..v)
	end	
	
	
	print("POST body contents:")
	for k,v in pairs(post_content) do
		print(k.." = "..v)
	end	
	
	
end

function http_get( req, uri )
	-- getQuery splits the request URI into useful peices
	-- it aslo decodes it:
	local get_query, get_path = getQuery(uri)

--	setContentType( req, uri )
		
	local r = {}
	if #get_path == 1 then
		-- domain name
		local domain_name = get_path[1]
		
		r = query("SELECT * FROM $domain_name;", { {["domain_name"] = domain_name} } )
		
	elseif #get_path == 2 then
		local domain_name = get_path[1]
		local item_name = get_path[2]
		
		r = query("SELECT * FROM $domain_name WHERE _name = $item_name;", { {["domain_name"] = domain_name, ["item_name"] = item_name} } )
	end
--[[
	for i,v in ipairs(get_path) do
		print(i,v)
	end
	
	print("Get query parts:")
	for k,v in pairs(get_query) do
		print(k.." = "..v)
	end	
]]--

--	addBody(req, [[ { "result":"failure", "items":[] } ]] )
	
	
	-- fake JSON generation:
	-- TODO: replace with a real JSON generator
	local item_str = "["
	local first_item = true
	for i,item in ipairs(r) do
		if not first_item then item_str = item_str .. "," end

		local first_attr = true
		item_str = item_str .. "{"
		for key,value in pairs(item) do
			if not first_attr then item_str = item_str .. "," end
			
			item_str = item_str .. [["]] ..key .. [[" = "]] .. value .. [["]]
			
			first_attr = false
		end
		item_str = item_str .. "}"		
		first_item = false
	end
	item_str = item_str .. "]"
	
	addBody(req,[[ { "result":"ok", "items":]]..item_str..[[ } ]])

--	addBody(req, [[ 
--<form method="POST" action="/">
--		<input type='text' name='somedata'/><br/>
--		<input type='submit' value='Submit'/>
--</form>		
--		]])

end

function getQuery( uri )
	s,e = uri:find("?",1,true)
	if s == nil then return {}, pathParts(uri) end
	path_part = uri:sub(1,s-1)
	query_part = uri:sub(s+1)
	-- decodeURIQuery takes the query portion of a URI and splits
	-- it into a decoded table of key/value pairs
	return decodeURIQuery(query_part), pathParts(path_part)
end

function pathParts( path )
	-- pulls apart a URI encoded path by splitting it by "/" then 
	-- taking the chunk and decoding it
	local path_parts = {}
	for field in path:gmatch"[^/]+" do table.insert(path_parts,decodeURI(field)) end
	return path_parts
end

function setContentType( req, uri )
	local user_agent = findHeader(req, "User-Agent" )
	if user_agent then
		local user_agent_parts = pathParts(user_agent) -- splits by "/" which is what we want
		if user_agent_parts[1] == "Mozilla" then
			-- mozilla browsers we want to show visible for debugging purposes:
			addHeader(req, "Content-Type", "text/plain" )
			return
		end
	end
	-- default is json
	addHeader(req, "Content-Type", "application/json" )
end
